package main

import (
	"fmt"
	"go_code/project1/pro/customerManager/model"
	"go_code/project1/pro/customerManager/service"
)

type view struct {
	service *service.UserService
}

func (this *view) mainMenu() {
	for loop := true; loop; {
		fmt.Println("----------------客服信息管理软件-----------------")
		fmt.Println("                 1. 添加客户 ")
		fmt.Println("                 2. 删除客户 ")
		fmt.Println("                 3. 修改客户 ")
		fmt.Println("                 4. 客户列表 ")
		fmt.Println("                 5. 退出 ")
		fmt.Print("请选择（1-5)： ")
		var key byte
		fmt.Scanf("%d\n", &key)
		switch key {
		case 1:
			fmt.Println("添加客户")
			this.add()
		case 2:
			fmt.Println("删除客户")
			this.delete()
		case 3:
			fmt.Println("修改客户")
			this.update()
		case 4:
			this.list()
		case 5:
			fmt.Println("您确认退出吗？ y/n")
			var ex byte
			for {
				fmt.Scanf("%c\n", &ex)
				if ex == 'y' || ex == 'n' {
					break
				}
				fmt.Println("please input valid key: y/n")
			}
			if ex == 'y' {
				loop = !loop
				break
			}
		default:
			fmt.Println("invalid input")
		}
	}
	fmt.Println("您已经退出本系统!!!!!!!!!!!")
}

func (this *view) list() {
	fmt.Println("----------------客户列表-----------------")
	fmt.Println("编号\t 姓名\t 性别\t 年龄\t 电话\t 邮箱")
	users := this.service.List()
	for _, user := range users {
		fmt.Println(user)
	}
}

func (this *view) add() {
	user := model.GetUser2()

	fmt.Print("input age：")
	fmt.Scanf("%d\n", &user.Age)

	fmt.Print("input name：")
	fmt.Scanf("%s\n", &user.Name)

	fmt.Print("input gender: ")
	fmt.Scanf("%s\n", &user.Gender)

	fmt.Print("input phone：")
	fmt.Scanf("%s\n", &user.Phone)

	fmt.Print("input Email：")
	fmt.Scanf("%s\n", &user.Email)

	this.service.Add(user)
	fmt.Println("添加成功")
	this.list()
}

func (this *view) update() {
	id := 1
	fmt.Print("please input update id : ")
	fmt.Scanf("%d\n", &id)
	user := model.GetUser2()

	fmt.Print("input age：")
	fmt.Scanf("%d\n", &user.Age)

	fmt.Print("input name：")
	fmt.Scanf("%s\n", &user.Name)

	fmt.Print("input gender: ")
	fmt.Scanf("%s\n", &user.Gender)

	fmt.Print("input phone：")
	fmt.Scanf("%s\n", &user.Phone)

	fmt.Print("input Email：")
	fmt.Scanf("%s\n", &user.Email)

	if this.service.Update(id, user) {
		fmt.Println("update succeed!")
		this.list()
	} else {
		fmt.Println("修改失败")
	}

}

func (this *view) delete() {
	id := -1
	fmt.Print("please input delete id：")
	fmt.Scanf("%d\n", &id)
	if this.service.Delete(id) {
		fmt.Println("delete succeed")
		this.list()
	} else {
		fmt.Println("fail to delete")
	}
}
func main() {
	v := &view{}
	v.service = service.GetUserService()
	v.mainMenu()
}
